<template>
  <div class="register">
    <main>
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        status-icon
        label-width="auto"
        size="large"
      >
        <el-form-item label="" prop="name">
          <el-input
            v-model="ruleForm.name"
            :prefix-icon="User"
            placeholder="请输入姓名"
          />
        </el-form-item>
        <el-form-item label="" prop="phone">
          <el-input
            v-model="ruleForm.phone"
            :prefix-icon="Iphone"
            placeholder="请输入手机号"
          />
        </el-form-item>
        <el-form-item label="" prop="password">
          <el-input
            v-model="ruleForm.password"
            type="password"
            show-password
            placeholder="请输入密码"
            autocomplete="off"
            :prefix-icon="Lock"
          />
        </el-form-item>
        <el-form-item label="" prop="confirmPassword">
          <el-input
            v-model="ruleForm.confirmPassword"
            type="password"
            show-password
            placeholder="请再次输入密码"
            autocomplete="off"
            :prefix-icon="Lock"
          />
        </el-form-item>
        <el-form-item label="" prop="code">
          <el-col :span="12" class="left-col">
            <el-input
              v-model="ruleForm.code"
              :prefix-icon="ScaleToOriginal"
              placeholder="请输入验证码"
            />
          </el-col>
          <el-col :span="12" class="right-col">
            <div v-html="userStore.captchaImage" @click="getCode"></div>
          </el-col>
        </el-form-item>

        <el-form-item label="" prop="checked">
          <el-checkbox v-model="ruleForm.checked" label="我已同意并阅读协议" />
          <span class="blur-tips left-m">用户协议</span>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm()"> 注册账号 </el-button>
        </el-form-item>
      </el-form>
    </main>
    <footer>
      <p>
        已有账号？
        <span class="blur-tips" @click="changePanel('login')">登 录</span>
      </p>
    </footer>
  </div>
</template>

<script setup lang="ts">
import { useUserStore } from "@/store";
import { User, Lock, ScaleToOriginal, Iphone } from "@element-plus/icons-vue";
const userStore = useUserStore();
const emit = defineEmits(["update:modelValue"]); //
const ruleForm = ref<any>({
  phone: "",
  password: "",
  name: "",
  checked: false,
  code: "",
  confirmPassword: "",
});
onMounted(() => {
  userStore.getCodeRequest();
});
const getCode = () => {
  userStore.getCodeRequest();
};
const submitForm = () => {
  // 用户名密码不能为空
  if (!ruleForm.value.name || !ruleForm.value.password) {
    ElMessage.error("用户名或密码不能为空");
    return;
  } // 手机号不能为空
  if (!ruleForm.value.phone) {
    ElMessage.error("手机号不能为空");
    return;
  }

  // 两次密码不一致
  if (ruleForm.value.password !== ruleForm.value.confirmPassword) {
    ElMessage.error("两次密码不一致");
    return;
  }

  if (ruleForm.value.code.toLowerCase() != userStore.imgCode.toLowerCase()) {
    ElMessage.error("验证码错误");
    // 字符转小写比较
    return;
  }
  if (!ruleForm.value.checked) {
    ElMessage.error("请先阅读用户协议");
    return;
  }
  userStore.registerRequest(ruleForm.value);
};
const changePanel = (type: "login" | "resetPwd") => {
  emit("update:modelValue", type);
};
</script>

<style lang="scss" scoped>
.left-m {
  display: inline-block;
  margin-left: 8px;
}
.register {
  height: 100%;
  .el-form-item {
    margin-bottom: 12px !important;
  }
}
</style>
